library(DeclareDesign)
library(tidyverse)
library(rdss)
library(RColorBrewer)


diagnosis_11.2 <- read_rds("diagnosis_objects/diagnosis_11.2.rds")

gg_df <-
  diagnosis_11.2 |>
  get_simulations() |> 
  mutate(significant = as.numeric(p.value <= 0.05),
         N = as.factor(N))

label_df <-
  tibble(N = as.factor(c(100, 500, 1000)),
         estimand = c(0.33, 0.23, 0.12),
         significant = c(0.2, 0.48, 0.85),
         label = paste0("N = ", N)
  )

gradient_color <- colorRampPalette(colors = c(dd_palette("dd_light_blue"), dd_palette("dd_dark_blue")))


g <-
  ggplot(gg_df, aes(estimand, significant, color = N, fill = N,  group = N)) +
  geom_smooth(method = 'loess',  formula = 'y ~ x', alpha = 0.1) +
  geom_text(data = label_df, aes(label = label)) +
  geom_hline(yintercept = 0.8,
             color = dd_palette("dd_light_gray"),
             linetype = "dashed") +
  theme_dd() +
  scale_color_manual(values = gradient_color(3)) +
  scale_fill_manual(values = gradient_color(3)) +
  coord_cartesian(ylim = c(0, 1), xlim = c(0, 0.5)) +
  labs(x = "True effect size",
       y = "Statistical power")

ggsave("figures/figure_11.2.svg",
       g,
       width = 6.5,
       height = 3.5)
ggsave("figures/figure_11.2.pdf",
       g,
       width = 6.5,
       height = 3.5)
